package community.hospital.service.domain.impl;



import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import community.hospital.entity.Order;
import community.hospital.entity.request.ListOrderRequest;
import community.hospital.mapper.OrderMapper;
import community.hospital.service.domain.OrderService;
import community.hospital.util.DateUtil;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 * @author yindingjie
 */
@Service
public class OrderServiceImpl extends ServiceImpl<OrderMapper, Order> implements OrderService {
    @Override
    public Boolean updateOrderState(Long orderId, Integer state) {
        return lambdaUpdate().eq(Order::getId, orderId)
                .set(Order::getState, state)
                .update();
    }

    @Override
    public Page<Order> listOrder(ListOrderRequest request) {
        Long userId = request.getUserId();
        Long scheduleId = request.getScheduleId();
        Integer state = request.getState();
        Date gmtCreate = request.getGmtCreate();
        Long doctorId = request.getDoctorId();
        Long hospitalId = request.getHospitalId();
        Long departmentId = request.getDepartmentId();
        Date startTime = request.getStartTime();
        Date endTime = request.getEndTime();
        Page<Order> orderPage = new Page<>(request.getPageNumber(), request.getPageSize());
        lambdaQuery().eq(userId != null, Order::getUserId, userId)
                .eq(scheduleId != null, Order::getScheduleId, scheduleId)
                .eq(state != null, Order::getState, state)
                .between(gmtCreate != null, Order::getGmtCreate, DateUtil.getStartDate(gmtCreate), DateUtil.getEndDate(gmtCreate))
                .eq(doctorId != null, Order::getDoctorId, doctorId)
                .eq(hospitalId != null, Order::getHospitalId, hospitalId)
                .ge(ObjectUtil.isNotNull(startTime),Order::getGmtCreate,startTime)
                .le(ObjectUtil.isNotNull(endTime),Order::getGmtCreate,endTime)
                .and(departmentId != null, t -> t.eq(Order::getDepartment1Id, departmentId).or().eq(Order::getDepartment2Id, departmentId))
                .orderByDesc(Order::getGmtCreate)
                .page(orderPage);

        return orderPage;
    }


    @Override
    public Long countByScheduleId(Long scheduleId) {
        return lambdaQuery()
                .eq(Order::getScheduleId, scheduleId)
                .count();
    }

    @Override
    public List<Order> getOrderByUserId(Long id) {

        return lambdaQuery().eq(Order::getUserId, id).list();
    }

    @Override
    public int getRank(Long scheduleId, Long userId) {
        List<Order> list = lambdaQuery().eq(Order::getScheduleId, scheduleId).orderByAsc(Order::getGmtCreate).list();
        for (int i = 1; i <= list.size(); i++) {
            if (list.get(i-1).getUserId().equals(userId)) {
                return i;
            }
        }
        return -1;
    }

    @Override
    public Order getAfterTomorrowOrder(Long id) {
//        lambdaQuery().eq(Order::getUserId,id)
        return null;
    }
}
